/*!
* 文件名     main.c
* 作者       聚沃科技
* 版本       V1.0
* 实验简介   TIMER-蜂鸣器PWM实验
*/

/*
* 版权说明
* 本程序只供学习使用，未经作者许可，不得用于其它任何用途
* GD32F303东方红开发板V1
* 在线购买：   http://juwo.taobao.com
* 技术交流：   http://www.gd32bbs.com/ask/
* 视频学习：   https://space.bilibili.com/475462605
* 微信公众号： gd32bbs
* 创建日期:    2023/8/30
* Copyright    苏州聚沃电子科技有限公司
* 版权所有，盗版必究。
*/

#include "gd32f30x.h"
#include <stdio.h>
#include "bsp_uart.h"
#include "bsp_led.h"
#include "bsp_timer.h"
#include "bsp_beep.h"
#include "bsp_led.h"
#include "driver_public.h"

uint16_t music[]=
{
    M6,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10,	//爱你孤身走暗巷
    H1,50,M7,50,H1,50,H2,50,M7,50,H1,50,H1,100,Z0,10, 	//爱你不跪的模样
    H1,50,H2,50,H3,50,H2,50,H3,50,H2,50,H3,100,H3,50,H3,50,H2,50,H3,100,H5,100,H3,100,Z0,10, //爱你对峙过绝望不肯哭一场    

//    //前奏
//    L7,25,M1,25,M2,25,M3,25,L3,50,M5,25,M3,25,M3,50,Z0,150, L7,25,M1,25,M2,25,M3,25,L2,50,M5,25,M3,25,M2,25,M3,25,M1,25,M2,25,L7,25,M1,25,L5,25,Z0,25, L7,25,M1,25,M2,25,M3,25,L3,50,M5,25,M3,75,Z0,150,L7,25,M1,25,M2,25,M3,25,L2,50,M5,25,M3,25,M2,25,M3,25,M1,25,M2,25,Z0,20,
//    //这一路上走走停停 顺着少年漂流的痕迹
//    M2,50,M2,50,M1,25,M2,50,M2,50,M1,25,M2,50,M3,50,M5,50,M3,50, M2,50,M2,50,M1,25,M2,50,M2,50,M1,25,M2,25,M3,25,M2,25,M1,25,L6,100,Z0,10,
//    //迈出车站的前一刻 竟有些犹豫
//    M2,50,M2,50,M1,25,M2,50,M2,50,M1,25,M2,50,M3,50,M5,50,M3,50, M2,50,M2,50,M3,25,M2,50,M1,50,M2,100,Z0,50,
//    //不仅笑着这近乡情怯 仍无法避免
//    M2,50,M2,50,M1,25,M2,50,M2,50,M1,25,M2,50,M3,50,M5,50,M3,50, M2,50,M2,50,M3,25,M2,50,M1,50,L6,100,Z0,10,
//    //而长野的天 依旧那么暖 风吹起了从前
//    M3,25,M2,25,M1,25,M2,25,M1,100, M3,25,M2,25,M1,25,M2,25,M1,50,M1,50, M5,25,M3,25,M2,25,M1,25,M2,25,M1,100,M1,150,Z0,30,
//    //从前初识这世间 万般留恋 看着天边似在眼前 也甘愿赴汤蹈火去走它一遍
//    M1,50,M2,50,M3,50,M1,50,M6,50,M5,25,M6,25,M6,50,M6,50, M1,25,M7,50,M6,25,M7,25,M7,100,Z0,5, M7,50,M6,25,M7,25,M7,50,M3,50,H1,25,H2,25,H1,25,M7,25,M6,50,M5,50, M6,50,M5,25,M6,25,M6,25,M5,25,M6,25,M5,25,M6,50,M5,25,M2,25,M2,25,M5,50,M5,50,M3,100,M3,100,Z0,25,
//    //如今走过这世间 万般留恋 翻过岁月不同侧脸 措不及防闯入你的笑颜
//    M1,50,M2,50,M3,50,M1,50,M6,50,M5,25,M6,25,M6,50,M6,50, M1,25,M7,50,M6,25,M7,25,M7,100,Z0,5, M7,50,M6,25,M7,25,M7,50,M3,50,H1,25,H2,25,H1,25,M7,25,M6,50,M5,50, M6,50,H3,25,H3,25,H3,50,M5,50,M6,50,H3,25,H3,25,H3,25,M5,50,M6,25,M6,100,M6,100,M6,100,Z0,25,
//    //我曾难自拔于世界之大 也沉溺于其中梦话
};

uint8_t int_flag=0;

void Music_switch(void)
{   
    static uint16_t i=0;
    
    
    bsp_beep_init(BEEP_CNT_HZ,music[i]); 
    buzzer_on();    
    buzzer_set(music[i],10);    
    i++;
    bsp_timer_init(10000,music[i]*60);
    i++; 

    if( i==(sizeof(music)/sizeof(music[0])) )
    {
        i=0;
        bsp_led_toggle(&LED1);
        int_flag=1;        
    }        
}
    
/*!
* 说明     main函数
* 输入     无
* 输出     无
* 返回值   无
*/

uint8_t uart_poll_buff[50],uart_dma_buff[50],uart_int_buff[50];
int main(void)
{
    uint16_t t=0;
    driver_init(); 
  
    bsp_uart_init(&BOARD_UART);
    bsp_led_group_init();  
    bsp_led_on(&LED0);
    bsp_led_off(&LED1);
        
    bsp_beep_init(BEEP_CNT_HZ,music[0]);    
    buzzer_on();
    
    TIMER_INT.timer_updata_callback=Music_switch;
    bsp_timer_init(BEEP_CNT_HZ,5000);
    
    printf_log("\r\n Bepp music play\r\n");
    
	while(1)
	{
        if(int_flag==1)
        {
            int_flag=0;
            t++;
            bsp_led_toggle(&LED0);            
            printf_log("\r\n Bepp music play loop %d\r\n",t);
        }
        
	}
}

